USE [ATAChietkhau]
GO

/****** Object:  View [dbo].[vwAllCustomers]    Script Date: 07/24/2010 11:20:33 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[vwAllCustomers]
AS
SELECT ao.AccountingObjectID AS KhachHangID
		                            , ao.AccountingObjectCode AS MaKH
		                            , ao.AccountingObjectName AS TenKH
		                            , 1 AS LEVEL, AccountingObjectID AS ParentID,
		                            BranchID, AOG.AccountingObjectGroupCode As BranchCode, Tel, Address
                                    FROM ATAHANOI.DBO.AccountingObject ao
                                    JOIN ATAHANOI.DBO.AccountingObjectGroup aog ON ao.AccountingObjectGroupID = aog.AccountingObjectGroupID
                                    WHERE ao.IsCustomer = 1 AND ao.IsEmployee = 0 AND ao.IsVendor = 0
                                    UNION ALL
                                    SELECT KhachHangID, Ma_KH AS MaKH, Ten_KH AS TenKH, 2 AS LEVEL, Parent_ID AS ParentID,
                                    kh.BranchID, BranchCode, So_DT_DK, Dia_Chi_TT
                                    FROM KhachHang KH
                                    JOIN vwMisaBranches BR ON KH.BranchID = BR.BranchID


GO


